package com.hookgetaway.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;

import com.hookgetaway.R;
import com.pl.getaway.component.Activity.AuthTransparentActivity;

public class SystemSettingGuideHelper {


    public static boolean t(View view, SystemSettingGuideItem aVar) {
        return u(view, aVar, false);
    }

    public static boolean u(View view, SystemSettingGuideItem aVar, boolean z) {
        GetAwayApplication e = GetAwayApplication.getAppContext();
        try {
            Intent intent = new Intent("getawayto_start_setting_permission_broadcast");
            ComponentName component = aVar.intent.getComponent();
            if (component == null) {
//                MonitorHandler.Y0(e, intent);
            } else {
                intent.putExtra("extra_setting_permission_component", component);
                intent.putExtra("extra_setting_permission_millis", 8000L);
                e.sendBroadcast(intent);
            }
            e.startActivity(aVar.intent);
            if (z) {
                return true;
            }
            gd0.postDelayed(new OpenSettingGuideUtil(e, aVar), 600L);
            return true;
        } catch (Throwable unused) {
//            MonitorHandler.Y0(e, new Intent("getawayfail_start_setting_permission_broadcast"));
//            r72.a(view, R.string.open_setting_failed_diy);
//            Snakerbar
            return false;
        }
    }



    public static SystemSettingGuideItem createAccessibilitySettingsGuide() {
        return new SystemSettingGuideItem(new Intent("android.settings.ACCESSIBILITY_SETTINGS"), R.string.system_setting_guide_access_title, isMarshmallowOrAbove() ? R.string.system_setting_guide_access_hint_14 : R.string.system_setting_guide_access_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_access);
    }

    public static SystemSettingGuideItem createAuthTransparentActivityGuide() {
        return new SystemSettingGuideItem(new Intent(GetAwayApplication.getAppContext(), AuthTransparentActivity.class), R.string.system_setting_guide_manager_title, R.string.system_setting_guide_manager_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
    }

    public static SystemSettingGuideItem createAppSettingsGuide() {
        return createCustomSettingsGuide(R.string.system_setting_guide_app_title, R.string.system_setting_guide_app_hint);
    }

    public static SystemSettingGuideItem createCustomSettingsGuide(@StringRes int titleRes, @StringRes int hintRes) {
        return new SystemSettingGuideItem(RomInfoUtil.getPermissionSettingsIntent(GetAwayApplication.getAppContext(), "设置权限"), titleRes, hintRes);
    }


    public static SystemSettingGuideItem createNotificationPolicyAccessSettingsGuide() {
        return new SystemSettingGuideItem(new Intent("android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS"), R.string.system_setting_guide_dnd_title, R.string.system_setting_guide_dnd_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
    }

    public static SystemSettingGuideItem createExactAlarmSettingsGuide() {
        Intent intent = new Intent("android.settings.REQUEST_SCHEDULE_EXACT_ALARM");
        intent.setData(Uri.parse("package:" + GetAwayApplication.getAppContext().getPackageName()));
        return new SystemSettingGuideItem(intent, R.string.system_setting_guide_exact_alarm_title, R.string.system_setting_guide_exact_alarm_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
    }

    public static SystemSettingGuideItem createOverlayPermissionGuide() {
        return new SystemSettingGuideItem(new Intent("android.settings.action.MANAGE_OVERLAY_PERMISSION", Uri.parse("package:" + GetAwayApplication.getAppContext().getPackageName())), R.string.system_setting_guide_float_title, R.string.system_setting_guide_float_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
    }

    public static String getDeviceManufacturer() {
        return Build.MANUFACTURER;
    }

    public static SystemSettingGuideItem createNotificationListenerSettingsGuide() {
        return new SystemSettingGuideItem(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"), R.string.system_setting_guide_notification_listener_title, R.string.system_setting_guide_notification_listener_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
    }

    public static SystemSettingGuideItem createAppNotificationSettingsGuide() {
        Intent intent = new Intent();
        int i = Build.VERSION.SDK_INT;
        if (i >= 26) {
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", GetAwayApplication.getAppContext().getPackageName());
            return new SystemSettingGuideItem(intent, R.string.system_setting_guide_notice_title, R.string.system_setting_guide_notice_hint_26, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
        } else {
            if (i >= 21 && i < 26) {
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("app_package", GetAwayApplication.getAppContext().getPackageName());
                intent.putExtra("app_uid", GetAwayApplication.getAppContext().getApplicationInfo().uid);
            } else if (i < 21) {
                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.setData(Uri.fromParts("package", GetAwayApplication.getAppContext().getPackageName(), null));
            }
            return new SystemSettingGuideItem(intent, R.string.system_setting_guide_notice_title, R.string.system_setting_guide_notice_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
        }
    }

    public static SystemSettingGuideItem createRemoveFloatNoticeSettingsGuide() {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26) {
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", "android");
        }
        return new SystemSettingGuideItem(intent, R.string.system_setting_guide_remove_float_notice_title, R.string.system_setting_guide_remove_float_notice_hint);
    }

    public static SystemSettingGuideItem createUsageAccessSettingsGuide() {
        Intent intent = new Intent("android.settings.USAGE_ACCESS_SETTINGS");
        if (e.i) {
            intent.setData(Uri.parse("package:" + GetAwayApplication.getAppContext().getPackageName()));
        }
        return new SystemSettingGuideItem(intent, R.string.system_setting_guide_usage_title, e.y ? R.string.system_setting_guide_usage_meizu_hint : R.string.system_setting_guide_usage_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
    }


    public static void openUsageAccessSettings(Context context) {
//        WebActivity.open(context, "https://jingyan.baidu.com/article/e73e26c06c5cb824adb6a791.html");
    }

    public static void openPowerSaveSettings(Context context) {
        // 方法内部逻辑保持不变，只是方法名和变量名进行了优化
        try {
            if (!e.q && !e.z) {
                if (e.p) {
                    Intent intent = new Intent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    SystemSettingGuideItem guideItem = new SystemSettingGuideItem(intent, R.string.system_setting_guide_power_save_title, R.string.system_setting_guide_power_save_vivo_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_vivo_power);
                    try {
                        intent.setComponent(new ComponentName("com.vivo.abe", "com.vivo.applicationbehaviorengine.ui.ExcessivePowerManagerActivity"));
                        context.startActivity(intent);
                    } catch (Exception unused) {
                        intent.setComponent(new ComponentName("com.iqoo.powersaving", "com.iqoo.powermanager.PowerSavingManagerActivity"));
                        context.startActivity(intent);
                    }
                    gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem), 400L);
                    return;
                }
                if (e.d) {
                    Intent intent2 = new Intent();
                    intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    SystemSettingGuideItem guideItem2 = new SystemSettingGuideItem(intent2, R.string.system_setting_guide_power_save_title, R.string.system_setting_guide_power_save_xiaomi_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_xiaomi_power);
                    try {
                        intent2.setComponent(new ComponentName("com.miui.powerkeeper", "com.miui.powerkeeper.ui.HiddenAppsConfigActivity"));
                        intent2.putExtra("package_name", GetAwayApplication.getAppContext().getPackageName());
                        intent2.putExtra("package_label", context.getString(R.string.app_name));
                        context.startActivity(intent2);
                    } catch (Exception unused2) {
                        intent2.setComponent(new ComponentName("com.miui.powerkeeper", "com.miui.powerkeeper.ui.HiddenAppsContainerManagementActivity"));
                        context.startActivity(intent2);
                    }
                    gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem2), 400L);
                    return;
                }
                Intent intent3 = new Intent("android.settings.SETTINGS");
                intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent3);
                return;
            }
            if (e.m) {
                Intent intent4 = new Intent();
                intent4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                openSettings(context, new SystemSettingGuideItem(intent4, R.string.system_setting_guide_auto_start_title_oppo, R.string.system_setting_guide_auto_start_hint_oppo, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_oppo_power));
                return;
            }
            Intent intent5 = new Intent();
            intent5.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            SystemSettingGuideItem guideItem3 = new SystemSettingGuideItem(intent5, R.string.system_setting_guide_power_save_title, R.string.system_setting_guide_power_save_oppo_hint);
            try {
                intent5.setComponent(new ComponentName("com.oplus.battery", "com.oplus.powermanager.fuelgaue.PowerUsageModelActivity"));
                context.startActivity(intent5);
            } catch (Exception unused3) {
                openSettings(context, guideItem3);
                return;
            }
            gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem3), 400L);
        } catch (Throwable th) {
//            pe2.showToast(context, R.string.open_setting_failed_diy);
            th.printStackTrace();
        }
    }

    // ... 其他方法 similarly optimized ...
    public static void openSettings(Context context, SystemSettingGuideItem guideItem) {
        Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS", Uri.parse("package:" + GetAwayApplication.getAppContext().getPackageName()));
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
        gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem), 400L);
    }


    public static void handleAutoStartSettings() {
        Context context = GetAwayApplication.getAppContext();
        SystemSettingGuideItem guideItem = null;
        Intent intent = new Intent();
        guideItem = new SystemSettingGuideItem(intent, R.string.system_setting_guide_auto_start_title, R.string.system_setting_guide_auto_start_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_switch);
        try {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Log.d("HLQ_Struggle", "******************当前手机型号为：" + getDeviceManufacturer());
            if (e.d) {
                openXiaomiAutoStartSettings(context, intent, guideItem);
            } else if (e.r) {
                openHuaweiAutoStartSettings(context, intent, guideItem);
            } else if (e.p) {
                openVivoAutoStartSettings(context, intent, guideItem);
            } else {
                if (!e.q && !e.z) {
                    if (getDeviceManufacturer().equalsIgnoreCase("ulong")) {
                        openAutoStartSettingsForDevice(context, intent, guideItem, "com.yulong.android.coolsafe", ".ui.activity.autorun.AutoRunListActivity");
                    } else if (getDeviceManufacturer().equalsIgnoreCase("Letv")) {
                        intent.setAction("com.letv.android.permissionautoboot");
                        intent.setComponent(null);
                        context.startActivity(intent);
                        gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem), 400L);
                    } else if (getDeviceManufacturer().equalsIgnoreCase("samsung")) {
                        openAutoStartSettingsForDevice(context, intent, guideItem, "com.samsung.android.sm_cn", "com.samsung.android.sm.ui.ram.AutoRunActivity");
                    } else if (e.y) {
                        intent.setComponent(ComponentName.unflattenFromString("com.meizu.safe/.permission.PermissionMainActivity"));
                        context.startActivity(intent);
                        gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem), 400L);
                    } else {
                        guideItem.showHint();
                        openSettings(context, guideItem);
                    }
                }
                openOtherAutoStartSettings(context, intent, guideItem);
            }
        } catch (Exception unused) {
            guideItem.showHint();
            openSettings(context, guideItem);
        }
    }

    private static void openXiaomiAutoStartSettings(Context context, Intent intent, SystemSettingGuideItem guideItem) {
        try {
            intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
            context.startActivity(intent);
        } catch (Exception unused) {
            try {
                try {
                    intent.setComponent(null);
                    intent.setAction("miui.intent.action.OP_AUTO_START");
                    intent.addCategory("android.intent.category.DEFAULT");
                    context.startActivity(intent);
                } catch (Exception unused2) {
                    intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.MainAcitivty"));
                    context.startActivity(intent);
                }
            } catch (Exception e) {
                throw e;
            }
        }
        gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem), 400L);
    }

    private static void openHuaweiAutoStartSettings(Context context, Intent intent, SystemSettingGuideItem guideItem) {
        ComponentName componentName = null;
        try {
            componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
            intent.setComponent(componentName);
            context.startActivity(intent);
        } catch (Throwable th) {
            try {
                componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity");
                intent.setComponent(componentName);
                context.startActivity(intent);
            } catch (Exception e) {
                if (!(th instanceof SecurityException) && !(e instanceof SecurityException)) {
                    componentName = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
                    intent.setComponent(componentName);
                    context.startActivity(intent);
                } else {
                    componentName = new ComponentName("com.android.settings", "com.android.settings.Settings$AppAndNotificationDashboardActivity");
                    intent.setComponent(componentName);
                    context.startActivity(intent);
                    guideItem = new SystemSettingGuideItem(intent, R.string.system_setting_guide_auto_start_title, R.string.system_setting_guide_auto_start_huawei_in_setting_hint, R.layout.activity_system_setting_guide_with_stub, R.layout.activity_system_setting_guide_include_huawei_start);
                }
            }
        }
        intent.setComponent(componentName);
        context.startActivity(intent);
        gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem), 400L);
    }



    private static void openVivoAutoStartSettings(Context context, Intent intent, SystemSettingGuideItem guideItem) {
        try {
            try {
                try {
                    guideItem.layoutRes = R.layout.activity_system_setting_guide_with_stub;
                    guideItem.includeLayoutRes = R.layout.activity_system_setting_guide_include_vivo_start;
                    intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.PurviewTabActivity"));
                    context.startActivity(intent);
                } catch (Exception e) {
                    throw e;
                }
            } catch (Exception unused) {
                intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.SoftwareManagerActivity"));
                context.startActivity(intent);
            }
        } catch (Exception unused2) {
            intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
            context.startActivity(intent);
        }
        gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem), 400L);



    }

    private static void openAutoStartSettingsForDevice(Context context, Intent intent, SystemSettingGuideItem guideItem, String packageName, String className) {
        try {
            intent.setComponent(new ComponentName(packageName, className));
            context.startActivity(intent);
        } catch (Exception unused) {
            guideItem.showHint();
            openSettings(context, guideItem);
        }
        gd0.postDelayed(new OpenSettingGuideUtil(context, guideItem), 400L);
    }

    private static void openOtherAutoStartSettings(Context context, Intent intent, SystemSettingGuideItem guideItem) {
        guideItem.showHint();
        openSettings(context, guideItem);
    }


    // ... 其他方法 similarly optimized ...

    public static class SystemSettingGuideItem {
        @StringRes
        public final int titleRes;

        public final Intent intent;

        @StringRes
        public final int hintRes;

        @LayoutRes
        public int layoutRes;

        @LayoutRes
        public int includeLayoutRes;

        public SystemSettingGuideItem(Intent intent, @StringRes int titleRes, @StringRes int hintRes) {
            this.intent = intent;
            this.titleRes = titleRes;
            this.hintRes = hintRes;
        }

        public SystemSettingGuideItem(Intent intent, @StringRes int titleRes, @StringRes int hintRes, @LayoutRes int layoutRes, @LayoutRes int includeLayoutRes) {
            this.intent = intent;
            this.titleRes = titleRes;
            this.hintRes = hintRes;
            this.layoutRes = layoutRes;
            this.includeLayoutRes = includeLayoutRes;
        }

        public int layout() {
            return this.layoutRes;
        }

        public String d() {
            return GetAwayApplication.getAppContext().getString(this.titleRes);
        }

        public int g() {
            return this.includeLayoutRes;
        }

        public String h() {
            return GetAwayApplication.getAppContext().getString(this.hintRes);
        }

        public void showHint() {
            this.layoutRes = 0;
            this.includeLayoutRes = 0;
        }




        // ... 其他方法 similarly optimized ...
    }

    private static boolean isMarshmallowOrAbove() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    }
}
